<template>
  <div class="comment">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :immediate-check="false"
      :finished="finished"
      @load="loadComments"
      finished-text="暂无更多评论！"
    >
      <div
        class="item van-hairline--bottom van-hairline--top"
        v-for="cmt in comments"
        :key="cmt.com_id.toString()"
      >
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="cmt.aut_photo"
        />
        <div class="info">
          <p>
            <span class="name">{{cmt.aut_name}}</span>
            <span style="float:right">
              <span class="van-icon van-icon-good-job-o zan"></span>
              <span class="count">{{cmt.like_count}}</span>
            </span>
          </p>
          <p>{{cmt.content}}</p>
          <p>
            <span class="time">{{cmt.pubdate | relTime}}</span>&nbsp;
            <van-tag
              plain
              @click="openReply(cmt.com_id.toString())"
            >{{cmt.reply_count}} 回复</van-tag>
          </p>
        </div>
      </div>
    </van-list>
    <!-- 底部输入框 -->
    <div class="reply-container van-hairline--top">
      <van-field
        v-model="value"
        :placeholder="showReply?'写回复...':'写评论...'"
      >
        <van-loading
          v-if="submiting"
          slot="button"
          type="spinner"
          size="16px"
        ></van-loading>
        <span
          class="submit"
          @click="submit"
          v-else
          slot="button"
        >提交</span>
      </van-field>
    </div>
    <!-- 回复 -->
    <van-action-sheet
      v-model="showReply"
      class="reply_dailog"
      title="回复评论"
    >
      <van-list
        v-model="reply.loading"
        :immediate-check="false"
        :finished="reply.finished"
        finished-text="暂无更多回复！"
      >
        <div
          class="item van-hairline--bottom van-hairline--top"
          v-for="item in reply.list"
          :key="item.com_id.toString()"
        >
          <van-image
            round
            width="1rem"
            height="1rem"
            fit="fill"
            :src="item.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{item.aut_name}}</span>
            </p>
            <p>{{item.content}}</p>
            <p>
              <span class="time">{{item.pubdate|relTime}}</span>
            </p>
          </div>
        </div>
      </van-list>
    </van-action-sheet>
  </div>
</template>

<script>
import { getComments, comment } from '@/api/article'
export default {
  data () {
    return {
      // 评论列表加载中状态
      loading: false,
      // 评论列表完全加载完毕状态
      finished: false,
      // 输入的内容
      value: '',
      // 提交 评论或者回复 中
      submiting: false,
      // 评论列表
      comments: [],
      // 显示回复对话框
      showReply: false,
      // 回复相关数据
      reply: {
        loading: false,
        finished: false,
        // 回复列表
        list: []
      }
    }
  },
  activated () {
    // 清空上一篇文章的评论
    this.comments = []
    // 开启加载中效果
    this.loading = true
    // 重置 是否完全加载完毕 状态
    this.finished = false
    // 重置 偏移量
    this.offset = null
    this.loadComments()
  },
  methods: {
    // 获取评论列表
    async loadComments () {
      const { status, data } = await getComments({
        type: 'a',
        source: this.$route.params.id,
        offset: this.offset

      })

      if (status === 200) {
        this.comments.push(...data.results)
        this.loading = false
        // 判断是否有数据
        if (data.last_id > data.end_id) {
          this.offset = data.last_id
        } else {
          this.finished = true
        }
        // 子组件渲染完后执行阅读记忆
        this.$nextTick(() => {
          this.$parent.goTop()
        })
      }
    },
    // 打开某个评论回复
    openReply (cmtId) {
      this.showReply = true
      // 当前点击的评论ID
      this.cmtId = cmtId
      // 清空之前的回复列表
      this.reply.list = []
      // 开启加载效果
      this.reply.loading = true
      // 重置 没有更多数据
      this.reply.finished = false
      this.replyOffset = null
      // 加载回复
      this.loadReplaysById()
    },
    // 获取某个评论的回复列表
    async loadReplaysById () {
      const { data } = await getComments({
        type: 'c',
        source: this.cmtId,
        offset: this.replyOffset
      })
      this.reply.list.push(...data.results)
      // 结束加载状态
      this.reply.loading = false
      // 判断是否还有数据
      if (data.last_id > data.end_id) {
        this.replyOffset = data.last_id
      } else {
        this.reply.finished = true
      }
    },
    // 对文章发表评论和回复文章下某条评论功能
    async submit () {
      // 判断是否有输入内容
      if (!this.value) return false
      // 开启提交效果
      this.submiting = true
      try {
        if (this.showReply) {
          // 回复文章某条评论
          const { data } = await comment(this.cmtId, this.value, this.$route.params.id)
          this.$toast.success('回复成功')
          // 在回复列表顶部追加一项数据 data.new_obj
          this.reply.list.unshift(data.new_obj)
          // 在当前回复的评论数据中 累加回复的数量
          const _comment = this.comments.find(item => item.com_id.toString() === this.cmtId)
          _comment.reply_count++
        } else {
          // 对文章评论
          const { data } = await comment(this.$route.params.id, this.value)
          this.$toast.success('评论成功')
          this.comments.unshift(data.new_obj)
        }
        // 关闭提交效果
        this.submiting = false
        // 清空内容
        this.value = ''
      } catch (e) {
        console.log(e)
        this.$toast.success('操作失败')
        this.submiting = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.comment {
  margin-top: 10px;
  /deep/ .item {
    display: flex;
    padding: 10px 0;
    width: 100%;
    overflow: hidden;
    .info {
      flex: 1;
      padding-left: 10px;
      .name {
        color: #069;
      }
      .zan {
        vertical-align: middle;
        padding-right: 2px;
      }
      .count {
        vertical-align: middle;
        font-size: 10px;
        color: #666;
      }
      .time {
        color: #666;
      }
      p {
        padding: 5px 0;
        margin: 0;
      }
    }
  }
  /deep/ .van-button:active::before {
    background: transparent;
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
  .submit {
    font-size: 12px;
    color: #3296fa;
  }
}
.van-popup--bottom {
  &.van-popup--round {
    border-radius: 0;
  }
}
.reply_dailog {
  height: 100%;
  max-height: 100%;
  display: flex;
  overflow: hidden;
  flex-direction: column;
  .van-action-sheet__header {
    background: #3296fa;
    color: #fff;
    .van-icon-close {
      color: #fff;
    }
  }
  .van-action-sheet__content {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px 44px;
  }
}
</style>
